package org.jeecg.crawler.other;


import org.jeecg.common.system.vo.DictModel;
import org.jeecg.modules.crawlerpaper.entity.CrawlerInfo;

import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.modules.crawlerpaper.service.ICrawlerInfoService;
import org.jeecg.modules.system.service.ISysDictService;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 国际在线
 */
@Slf4j
public class CCTVCrawlerAJob implements Job {

    /**
     * 若参数变量名修改 QuartzJobController中也需对应修改
     */
    private String parameter;

    public void setParameter(String parameter) {
        this.parameter = parameter;
    }

        @Autowired
        private ICrawlerInfoService crawlerInfoService;

      @Autowired
      private ISysDictService sysDictService;

    @SneakyThrows
    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        List<DictModel> keywords = sysDictService.queryDictItemsByCode("keywords");
        if(!CollectionUtils.isEmpty(keywords)){
            List<String> collect = keywords.stream().map(DictModel::getValue).collect(Collectors.toList());
            List<CrawlerInfo> crawlerInfoList = SearchNewsCrawler.getCCTVNews(collect, Integer.valueOf(parameter));
            crawlerInfoService.saveOrUpdateBatch(crawlerInfoList);
        }

    }





}
